from models.account import Account
from models.token import Token
from models.exception import ParkAdminErrorCode, ParkAdminException

def login(login : Account):
    token = Token.CreateToken(login)
    if not token:
        raise ParkAdminException(ParkAdminErrorCode.InvalidAccountOrPassword)
    return {"code": 20000, "data": { "token" : str(token)}}

def logout(token : str):
    return {"code": 20000, "data": "success"}

def accessToken(request):
    token : Token = Token.Parse(request.headers["X-Token"] if "X-Token" in request.headers else "")
    if not token:
        raise ParkAdminException(ParkAdminErrorCode.InvalidToken)
    if token.isExpired():
        raise ParkAdminException(ParkAdminErrorCode.ExpiredToken)
    return token.getUser()
